<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Linux下安装ElasticSearch, ForageAhead">
    <meta name="description" content="最怕你一生碌碌无为，还安慰自己平凡可贵！">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>Linux下安装ElasticSearch | ForageAhead</title>
    <link rel="icon" type="image/png" href="/hexo-blog/favicon.png">

    <link rel="stylesheet" type="text/css" href="/hexo-blog/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/hexo-blog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/hexo-blog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/hexo-blog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/hexo-blog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/hexo-blog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/hexo-blog/css/my.css">

    <script src="/hexo-blog/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.3.0">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="alternate" href="/hexo-blog/atom.xml" title="ForageAhead" type="application/atom+xml">
</head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/hexo-blog/" class="waves-effect waves-light">
                    
                    <img src="/hexo-blog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">ForageAhead</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/hexo-blog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/hexo-blog/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/hexo-blog/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/hexo-blog/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/hexo-blog/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/hexo-blog/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/hexo-blog/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/hexo-blog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">ForageAhead</div>
        <div class="logo-desc">
            
            最怕你一生碌碌无为，还安慰自己平凡可贵！
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/hexo-blog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/hexo-blog/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/hexo-blog/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/hexo-blog/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/hexo-blog/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/hexo-blog/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/hexo-blog/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/CodeFarmer1999" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/CodeFarmer1999" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    
<script src="/hexo-blog/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/hexo-blog/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('/hexo-blog/medias/featureimages/10.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">Linux下安装ElasticSearch</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/hexo-blog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/hexo-blog/tags/ElasticSearch/">
                                <span class="chip bg-color">ElasticSearch</span>
                            </a>
                        
                            <a href="/hexo-blog/tags/Linux/">
                                <span class="chip bg-color">Linux</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/hexo-blog/categories/%E4%B8%AD%E9%97%B4%E4%BB%B6/" class="post-category">
                                中间件
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2021-02-05
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2021-02-05
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    5.6k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    23 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/hexo-blog/libs/prism/prism.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="0-Elaticsearch-简介"><a href="#0-Elaticsearch-简介" class="headerlink" title="0. Elaticsearch 简介"></a>0. Elaticsearch 简介</h2><p><strong>Elaticsearch</strong>，简称为<strong>ES</strong>，<strong>ES</strong>是一个开源的<strong>高扩展的分布式全文检索引擎</strong>，它可以近乎<strong>实时的存储、检索数据</strong>；本身扩展性很好，可以扩展到上百台服务器，处理 PB 级别（大数据时代）的数据。<strong>ES</strong>由 Java 语言开发并使用 <strong>Lucene</strong> 作为其核心来实现所有索引和搜索的功能，但是它的目的是通过简单的 RESTFULL API 来隐藏 <strong>Lucene</strong> 的复杂性，从而让全文搜索变得简单。据国际权威的数据库产品评测机构 DB Engines 的统计，在2016 年1月，<strong>ElasticSearch</strong> 已超过 <strong>Solr</strong> 等，成为排名第一的搜索引擎类应用。  </p>
<p><strong>ES 适用于全文搜索、结构化搜索、分析以及将这三者混合使用</strong>  ！</p>
<hr>
<h2 id="1-ES-的下载安装-Linux-下安装"><a href="#1-ES-的下载安装-Linux-下安装" class="headerlink" title="1. ES 的下载安装(Linux 下安装)"></a>1. ES 的下载安装(Linux 下安装)</h2><h3 id="1-1-下载ES"><a href="#1-1-下载ES" class="headerlink" title="1.1 下载ES"></a>1.1 下载ES</h3><p><strong>要求</strong>：JDK版本最低为1.8 ！系统中已经配置好 JAVA 环境：<a target="_blank" rel="noopener" href="https://blog.csdn.net/z69183787/article/details/78126238">https://blog.csdn.net/z69183787/article/details/78126238</a></p>
<ul>
<li><p>官网地址：<a target="_blank" rel="noopener" href="https://www.elastic.co/cn/">https://www.elastic.co/cn/</a></p>
</li>
<li><p>官方文档地址：<a target="_blank" rel="noopener" href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html</a></p>
</li>
<li><p>下载地址：<a target="_blank" rel="noopener" href="https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-6-1">https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-6-1</a></p>
</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/20201130143712228.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h3 id="1-2-安装ES"><a href="#1-2-安装ES" class="headerlink" title="1.2 安装ES"></a>1.2 安装ES</h3><h4 id="ES压缩包上传"><a href="#ES压缩包上传" class="headerlink" title="ES压缩包上传"></a>ES压缩包上传</h4><p>我们将<strong>ES</strong> 的压缩包下载到本机电脑后，通过 XFTP 这个上传到服务器或者虚拟机指定文件夹位置：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130144138477.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>我上传的位置是自己习惯的位置：<a href="">/usr/local/src/software</a></p>
<p>接下来使用 XShell 工具连接到服务器或者虚拟机(我使用的是阿里云服务器)</p>
<h4 id="ES压缩包解压"><a href="#ES压缩包解压" class="headerlink" title="ES压缩包解压"></a>ES压缩包解压</h4><p>解压命令：<code>tar -zxvf elasticsearch-7.6.1-linux-x86_64.tar.gz </code></p>
<p>解压后的文件夹修改下名字，方便输入：<code>mv elasticsearch-7.6.1 elasticsearch7</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130145344888.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h4 id="ES目录介绍"><a href="#ES目录介绍" class="headerlink" title="ES目录介绍"></a>ES目录介绍</h4><pre class="line-numbers language-json" data-language="json"><code class="language-json">bin<span class="token operator">:</span>下面存放着Es启动文件         elasticsearch.bat/elasticsearch
config：配置目录
data<span class="token operator">:</span>数据目录
jdk、lib：Java运行环境以及依赖包
logs<span class="token operator">:</span>日志目录
modules、plugins：模块及插件目录，head插件可以存放在plugins目录下<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="ES相关配置"><a href="#ES相关配置" class="headerlink" title="ES相关配置"></a>ES相关配置</h4><h5 id="基础配置"><a href="#基础配置" class="headerlink" title="基础配置"></a>基础配置</h5><p><strong>ES</strong> 本身其实也相当于是一个数据库，为此，我们在 elasticsearch7 文件夹下自己建一个 data 文件夹，用于存放数据：<code>mdkir data</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130145934187.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>进入 data 文件夹下，执行 <code>pwd</code> 命令拷贝下该文件夹的路径，下面配置要用到！</p>
<p>我的 data 路径：<a href="">/usr/local/src/software/elasticsearch7/data</a></p>
<p>修改配置文件<strong>elasticsearch.yml</strong>，我们进入elasticsearch7 这个文件夹下的config 文件夹，编辑 <strong>elasticsearch.yml</strong> 文件：<code>vim elasticsearch.yml</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130150348343.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><img src="https://img-blog.csdnimg.cn/20201130160519464.png" alt="在这里插入图片描述"></p>
<p>下面 <code>wq</code> 保存退出，回到elasticsearch7  文件夹下，进入 logs 文件夹，<code>pwd</code> 查看该文件夹路径，并复制！</p>
<p>如：<a href="">/usr/local/src/software/elasticsearch7/logs</a>，然后重新到 config 目录下编辑<strong>elasticsearch.yml</strong> 文件：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130150838382.png" alt="在这里插入图片描述"></p>
<h5 id="跨域配置"><a href="#跨域配置" class="headerlink" title="跨域配置"></a>跨域配置</h5><p><strong>在config目录下的elasticsearch.yml文件末尾添加跨域允许：</strong></p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token comment"># 跨域问题</span>
<span class="token key atrule">http.cors.enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
<span class="token key atrule">http.cors.allow-origin</span><span class="token punctuation">:</span> <span class="token string">"*"</span>
<span class="token key atrule">http.cors.allow-headers</span><span class="token punctuation">:</span> Authorization<span class="token punctuation">,</span>X<span class="token punctuation">-</span>Requested<span class="token punctuation">-</span>With<span class="token punctuation">,</span>Content<span class="token punctuation">-</span>Length<span class="token punctuation">,</span>Content<span class="token punctuation">-</span>Type<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="配置总结"><a href="#配置总结" class="headerlink" title="配置总结"></a>配置总结</h5><table>
<thead>
<tr>
<th>属性名</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td><strong>cluster.name</strong></td>
<td>配置elasticsearch的集群名称，默认是elasticsearch。建议修改成一个有意义的名称。</td>
</tr>
<tr>
<td><strong>node.name</strong></td>
<td>节点名，es会默认随机指定一个名字，建议指定一个有意义的名称，方便管理</td>
</tr>
<tr>
<td><strong>path.conf</strong></td>
<td>设置配置文件的存储路径，tar或zip包安装默认在es根目录下的config文件夹，rpm安装默认在 /etc/elasticsearch</td>
</tr>
<tr>
<td><strong>path.data</strong></td>
<td>设置索引数据的存储路径，默认是es根目录下的data文件夹，可以设置多个存储路径，用逗号隔开</td>
</tr>
<tr>
<td><strong>path.logs</strong></td>
<td>设置日志文件的存储路径，默认是es根目录下的logs文件夹</td>
</tr>
<tr>
<td><strong>path.plugins</strong></td>
<td>设置插件的存放路径，默认是es根目录下的plugins文件夹</td>
</tr>
<tr>
<td><strong>bootstrap.memory_lock</strong></td>
<td>设置为true可以锁住ES使用的内存，避免内存进行swap</td>
</tr>
<tr>
<td><strong>network.host</strong></td>
<td>设置bind_host和publish_host，设置为0.0.0.0允许外网访问</td>
</tr>
<tr>
<td><strong>http.port</strong></td>
<td>设置对外服务的http端口，默认为9200。</td>
</tr>
<tr>
<td><strong>transport.tcp.port</strong></td>
<td>集群结点之间通信端口</td>
</tr>
<tr>
<td><strong>discovery.zen.ping.timeout</strong></td>
<td>设置ES自动发现节点连接超时的时间，默认为3秒，如果网络延迟高可设置大些</td>
</tr>
<tr>
<td><strong>discovery.zen.minimum_master_nodes</strong></td>
<td>主结点数量的最少值 ,此值的公式为：<code>(master_eligible_nodes/2)+1</code> ，比如：有3个符合要求的主结点，那么这里要设置为2</td>
</tr>
</tbody></table>
<p>具体配置流程还可以参考这篇文章：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_43019282/article/details/105352982">https://blog.csdn.net/weixin_43019282/article/details/105352982</a></p>
<h2 id="2-ES-的分词器插件"><a href="#2-ES-的分词器插件" class="headerlink" title="2. ES 的分词器插件"></a>2. ES 的分词器插件</h2><blockquote>
<p>ES 自己默认带有分词器，但是支持的是英文分词，所以我们要安装一个可以对中文分词的插件</p>
</blockquote>
<h3 id="2-1-IK-分词器下载"><a href="#2-1-IK-分词器下载" class="headerlink" title="2.1 IK 分词器下载"></a>2.1 IK 分词器下载</h3><p>IK 分词器 Github 地址：<a target="_blank" rel="noopener" href="https://github.com/medcl/elasticsearch-analysis-ik">https://github.com/medcl/elasticsearch-analysis-ik</a></p>
<p><img src="https://img-blog.csdnimg.cn/20201130151441806.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><strong>注意</strong> ：IK 分词器的版本需要严格对应 ES的版本，所以我都使用的是 7.6.1 这个版本，IK 7.6.1下载地址<a target="_blank" rel="noopener" href="https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.6.1">https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.6.1</a></p>
<p><img src="https://img-blog.csdnimg.cn/20201130151745364.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>同样的，下载好后，我们使用XFTP 这个工具将其上传到服务器或者虚拟机指定文件夹，我个人还是习惯放到：</p>
<p><a href="/usr/local/src/software">/usr/local/src/software</a> 这个文件夹下：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130151924942.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>然后我们就可以使用Xshell 对其进行操作：</p>
<h3 id="2-2-IK-分词器压缩包解压到ES-插件文件夹下"><a href="#2-2-IK-分词器压缩包解压到ES-插件文件夹下" class="headerlink" title="2.2 IK 分词器压缩包解压到ES 插件文件夹下"></a>2.2 IK 分词器压缩包解压到ES 插件文件夹下</h3><ul>
<li>首先我们进入 elasticsearch7 文件夹下的 plugins 文件夹下，新建一个 ik 文件夹，用于存放 IK 分词器插件：</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/20201130152400620.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>ik 文件夹的路径为 <a href="">/usr/local/src/software/elasticsearch7/plugins/ik</a></p>
<ul>
<li>然后我们在 software 文件夹下，将 IK 分词器的压缩包解压到 ik 文件中去，命令为：<code>unzip elasticsearch-analysis-ik-7.6.1.zip -d /usr/local/src/software/elasticsearch7/plugins/ik</code>，如果出现以下问题：</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/20201130152726743.png" alt="在这里插入图片描述"></p>
<p>需要先下载 <strong>unzip</strong> 命令：<code>yum install unzip zip</code>，然后再执行解压命令：<code>unzip elasticsearch-analysis-ik-7.6.1.zip -d /usr/local/src/software/elasticsearch7/plugins/ik</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130152935199.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>然后我们到 ik 目录下检查是否解压成功：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130153051589.png" alt="在这里插入图片描述"></p>
<h3 id="2-4-IK-分词器的配置文件"><a href="#2-4-IK-分词器的配置文件" class="headerlink" title="2.4 IK 分词器的配置文件"></a>2.4 IK 分词器的配置文件</h3><h4 id="默认分词配置"><a href="#默认分词配置" class="headerlink" title="默认分词配置"></a>默认分词配置</h4><p>进入ik 目录下的 config 目录：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130153550761.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>进入查看下 <strong>extra_main.doc</strong> 配置文件：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130153654909.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>可以看出，汉字相关的分词配置都是在配置文件中一个一个的枚举出来的！</p>
<h4 id="扩展分词配置"><a href="#扩展分词配置" class="headerlink" title="扩展分词配置"></a>扩展分词配置</h4><p>如果新出一些网络热词，我们想对其进行分词配置，我们需要到 ik 文件夹 中 config 文件夹下的<strong>IKAnalyzer.cfg.xml</strong> 去配置：<code>vim IKAnalyzer.cfg.xml </code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130154108533.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>如果需要加一些额外的网络新词，可以把这些词放到 自己新建的 ext_dict 文件中，每个词直接都要换行，就像<strong>extra_main.doc</strong> 文件中的格式一样！</p>
<h2 id="3-启动与关闭ES"><a href="#3-启动与关闭ES" class="headerlink" title="3. 启动与关闭ES"></a>3. 启动与关闭ES</h2><blockquote>
<p>为了测试请求方便，我们需要下载 Postman 来模拟请求发送：<a target="_blank" rel="noopener" href="https://www.postman.com/">Postman 官网</a>，这个软件开发中很常用，我这里就不再赘述下载和使用步骤了！当然不一定非要使用Postman 模拟请求，其他类似的工具或者命令行都可以模拟，根据个人习惯选择把！</p>
</blockquote>
<h3 id="3-1-ES-服务启动与关闭"><a href="#3-1-ES-服务启动与关闭" class="headerlink" title="3.1 ES 服务启动与关闭"></a>3.1 ES 服务启动与关闭</h3><p>进入bin 目录，执行命令：<code>./elasticsearch</code> 运行<strong>ES</strong> 服务！后台运行命令：<code>./elasticsearch -d </code></p>
<p>关闭ES 服务：<code>ps -ef|grep elastic</code> 查看进程，并使用 <code>kill -9 进程id</code> 来结束进程!</p>
<h4 id="可能出现问题一"><a href="#可能出现问题一" class="headerlink" title="可能出现问题一"></a>可能出现问题一</h4><p>在服务器上跑 <strong>ES</strong> 如果启动时出现以下错误：</p>
<p><img src="https://img-blog.csdnimg.cn/20201201090754617.png" alt="在这里插入图片描述"></p>
<p>这种情况说明，内存不够了，服务器(学生机1核2g)内存较小，不足以启动ES服务，因为ES 默认启动内存大小就要求2g！</p>
<h5 id="解决方案一"><a href="#解决方案一" class="headerlink" title="解决方案一"></a>解决方案一</h5><p>可以修改 config 下的 jvm.options 配置文件，将运行大小 2g 修改为 1g(还是不行的话，再小设置到256m)：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130164851993.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h5 id="解决方案二"><a href="#解决方案二" class="headerlink" title="解决方案二"></a>解决方案二</h5><h5 id="使用top命令查看占用内存多的进程将其结束："><a href="#使用top命令查看占用内存多的进程将其结束：" class="headerlink" title="使用top命令查看占用内存多的进程将其结束："></a>使用top命令查看占用内存多的进程将其结束：</h5><p><img src="https://img-blog.csdnimg.cn/20201130164600216.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p><strong>踩坑</strong>，因为我这个学生机上有正在运行的 java 项目，包括 zookeeper 和 kafka等等服务，所以我换台学生机测试</p>
<h4 id="可能出现问题二"><a href="#可能出现问题二" class="headerlink" title="可能出现问题二"></a>可能出现问题二</h4><p>如果出现下面这种问题：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130165307178.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>说明不能以root 用户去启动 <strong>ES</strong> 服务！</p>
<h5 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h5><ul>
<li><p>新建一个用户elasticsearch，命令： <code>adduser elasticsearch</code></p>
</li>
<li><p>在software 目录下 赋予 elasticsearch7 这个文件夹的权限给 elasticsearch 用户，命令：<code>chown -R elasticsearch elasticsearch7</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130170009613.png" alt="在这里插入图片描述"></p>
</li>
<li><p><code>su elasticsearch</code> 命令，切换到 elasticsearch 用户，并重新到 bin 目录下执行 ES服务</p>
</li>
</ul>
<h4 id="可能出现问题三"><a href="#可能出现问题三" class="headerlink" title="可能出现问题三"></a>可能出现问题三</h4><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">ERROR: bootstrap checks failed
max virtual memory areas vm.max_map_count <span class="token punctuation">[</span><span class="token number">65530</span><span class="token punctuation">]</span> is too low, increase to at least <span class="token punctuation">[</span><span class="token number">262144</span><span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h5 id="解决方案-1"><a href="#解决方案-1" class="headerlink" title="解决方案"></a>解决方案</h5><ul>
<li><p>修改配置sysctl.conf<br><code>vim /etc/sysctl.conf</code></p>
</li>
<li><p>在最后一行添加下面配置：<br><code>vm.max_map_count=655360</code></p>
</li>
<li><p>保存后退出并执行命令：<br><code>sysctl -p</code></p>
</li>
</ul>
<p>然后，重新启动 <strong>elasticsearch</strong>，即可启动成功。</p>
<h4 id="可能出现问题四"><a href="#可能出现问题四" class="headerlink" title="可能出现问题四"></a>可能出现问题四</h4><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">ERROR: <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> bootstrap checks failed
<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>: the default discovery settings are unsuitable <span class="token keyword">for</span> production use<span class="token punctuation">;</span> at least one of <span class="token punctuation">[</span>discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes<span class="token punctuation">]</span> must be configured<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h5 id="解决方案-2"><a href="#解决方案-2" class="headerlink" title="解决方案"></a>解决方案</h5><p>在<code>elasticsearch</code>的<code>config</code>目录下，修改<code>elasticsearch.yml</code>配置文件，将下面的配置加入到该配置文件中：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130173112959.png" alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/20201130173217296.png" alt="在这里插入图片描述"></p>
<h3 id="3-2-阿里云开放安全组端口"><a href="#3-2-阿里云开放安全组端口" class="headerlink" title="3.2 阿里云开放安全组端口"></a>3.2 阿里云开放安全组端口</h3><p><img src="https://img-blog.csdnimg.cn/20201130173651668.png" alt="在这里插入图片描述"></p>
<h3 id="3-3-测试访问ES服务"><a href="#3-3-测试访问ES服务" class="headerlink" title="3.3 测试访问ES服务"></a>3.3 测试访问ES服务</h3><p><img src="https://img-blog.csdnimg.cn/20201130180713772.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>访问成功，说明ES启动成功了！</p>
<h2 id="4-ES中基本概念"><a href="#4-ES中基本概念" class="headerlink" title="4. ES中基本概念"></a>4. ES中基本概念</h2><h3 id="4-0关系型数据库和ES对比"><a href="#4-0关系型数据库和ES对比" class="headerlink" title="4.0关系型数据库和ES对比"></a>4.0关系型数据库和ES对比</h3><table>
<thead>
<tr>
<th>Relational DB</th>
<th>Elasticsearch</th>
</tr>
</thead>
<tbody><tr>
<td>数据库(database)</td>
<td>索引(indices)</td>
</tr>
<tr>
<td>表(tables)</td>
<td>类型(types)</td>
</tr>
<tr>
<td>行(一条记录)(rows)</td>
<td>文档(documents)</td>
</tr>
<tr>
<td>字段(columns)</td>
<td>属性(fields)</td>
</tr>
</tbody></table>
<ul>
<li><p>索引（indices）—————— <strong>Databases</strong> 数据库</p>
</li>
<li><p>类型（type）—————— <strong>Table</strong> 数据表</p>
</li>
<li><p>文档（Document）—————— <strong>Row</strong> 行</p>
</li>
<li><p>字段（Field）—————— <strong>Columns</strong> 列 </p>
</li>
</ul>
<h3 id="4-1-接近实时-NRT-Near-Real-Time"><a href="#4-1-接近实时-NRT-Near-Real-Time" class="headerlink" title="4.1 接近实时(NRT Near Real Time )"></a>4.1 接近实时(NRT Near Real Time )</h3><p><strong>Elasticsearch</strong>是一个接近实时的搜索平台。这意味着，从索引一个文档直到这个文档能够被搜索到有一个轻微的延迟(通常是1秒内)</p>
<h3 id="4-2-索引-index"><a href="#4-2-索引-index" class="headerlink" title="4.2 索引(index)"></a>4.2 索引(index)</h3><p><strong>一个索引就是一个拥有几分相似特征的文档的集合</strong>。比如说，你可以有一个客户数据的索引，另一个产品目录的索引，还有一个订单数据的索引。<strong>一个索引由一个名字来标识(必须全部是小写字母的)**，</strong>并且当我们要对这个索引中的文档进行索引、搜索、更新和删除的时候，都要使用到这个名字<strong>。</strong>索引类似于关系型数据库中Database 的概念**。在一个集群中，如果你想，可以定义任意多的索引。 </p>
<h3 id="4-3-类型-type"><a href="#4-3-类型-type" class="headerlink" title="4.3 类型(type)"></a>4.3 类型(type)</h3><p><strong>在一个索引中，你可以定义一种或多种类型</strong>。一个类型是你的索引的一个逻辑上的分类/分区，其语义完全由你来定。通常，会为具有一组共同字段的文档定义一个类型。比如说，我们假设你运营一个博客平台并且将你所有的数 据存储到一个索引中。在这个索引中，你可以为用户数据定义一个类型，为博客数据定义另一个类型，当然，也可 以为评论数据定义另一个类型。<strong>类型类似于关系型数据库中Table的概念</strong>。 </p>
<p><strong>NOTE: 在5.x版本以前可以在一个索引中定义多个类型,6.x之后版本也可以使用,但是不推荐,在7~8.x版本中彻底移除一个索引中创建多个类型</strong></p>
<h3 id="4-4-映射-Mapping"><a href="#4-4-映射-Mapping" class="headerlink" title="4.4 映射(Mapping)"></a>4.4 映射(Mapping)</h3><p><strong>Mapping</strong>是ES中的一个很重要的内容，<strong>它类似于传统关系型数据中table的schema，用于定义一个索引(index)中的类型(type)的数据的结构</strong>。 在ES中，我们可以手动创建type(相当于table)和mapping(相关与schema),也可以采用默认创建方式。<strong>在默认配置下，ES可以根据插入的数据自动地创建type及其mapping。 mapping中主要包括字段名、字段数据类型和字段索引类型</strong> </p>
<h3 id="4-5-文档-document"><a href="#4-5-文档-document" class="headerlink" title="4.5 文档(document)"></a>4.5 文档(document)</h3><p><strong>一个文档是一个可被索引的基础信息单元，类似于表中的一条记录。</strong>比如，你可以拥有某一个员工的文档,也可以拥有某个商品的一个文档。文档以采用了轻量级的数据交换格式JSON(Javascript Object Notation)来表示。 </p>
<h2 id="5-ES-常用命令"><a href="#5-ES-常用命令" class="headerlink" title="5. ES 常用命令"></a>5. ES 常用命令</h2><h3 id="5-1-索引操作"><a href="#5-1-索引操作" class="headerlink" title="5.1 索引操作"></a>5.1 索引操作</h3><p>我们先使用命令行的方式 curl 发送请求：</p>
<h4 id="查看ES服务健康状态："><a href="#查看ES服务健康状态：" class="headerlink" title="查看ES服务健康状态："></a>查看ES服务健康状态：</h4><p><code>curl -X GET "ip地址:9200/_cat/health?v"</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130181527721.png" alt="在这里插入图片描述"></p>
<h4 id="查看ES服务中的节点："><a href="#查看ES服务中的节点：" class="headerlink" title="查看ES服务中的节点："></a>查看ES服务中的节点：</h4><p><code>curl -X GET "ip地址:9200/_cat/nodes?v</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130181705215.png" alt="在这里插入图片描述"></p>
<h4 id="查看ES服务中的所有索引："><a href="#查看ES服务中的所有索引：" class="headerlink" title="查看ES服务中的所有索引："></a>查看ES服务中的所有索引：</h4><p><code>curl -X GET "ip地址:9200/_cat/indices?v"</code></p>
<p><img src="https://img-blog.csdnimg.cn/2020113018192840.png" alt="在这里插入图片描述"></p>
<h4 id="新增索引："><a href="#新增索引：" class="headerlink" title="新增索引："></a>新增索引：</h4><p><img src="https://img-blog.csdnimg.cn/2020113018253869.png" alt="在这里插入图片描述"></p>
<h4 id="删除索引："><a href="#删除索引：" class="headerlink" title="删除索引："></a>删除索引：</h4><p><img src="https://img-blog.csdnimg.cn/20201130182710512.png" alt="在这里插入图片描述"></p>
<p>从上面我们也能发现，每次使用 <code>curl</code> 去手动输入请求<strong>ES</strong> 服务不太方便，所以我们用 Postman 请求模拟工具来替代手动输入！</p>
<h4 id="新建索引："><a href="#新建索引：" class="headerlink" title="新建索引："></a>新建索引：</h4><p><img src="https://img-blog.csdnimg.cn/20201130183438397.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h4 id="新建文档并指定id："><a href="#新建文档并指定id：" class="headerlink" title="新建文档并指定id："></a>新建文档并指定id：</h4><p><code>PUT IP地址:9200/myindex/_doc/1</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130190854806.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h4 id="根据id查询数据："><a href="#根据id查询数据：" class="headerlink" title="根据id查询数据："></a>根据id查询数据：</h4><p><code>GET IP地址:9200/myindex/_doc/1 </code></p>
<p><img src="https://img-blog.csdnimg.cn/2020113019101515.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h4 id="根据id修改数据："><a href="#根据id修改数据：" class="headerlink" title="根据id修改数据："></a>根据id修改数据：</h4><p><code>PUT IP地址:9200/myindex/_doc/1</code></p>
<p>修改数据和新建一个文档一样，都是自定id 然后PUT 提交数据：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130191344913.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>根据id删除数据：</p>
<p><code>DELETE IP地址:9200/myindex/_doc/1</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130191504417.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h3 id="5-2-索引搜索"><a href="#5-2-索引搜索" class="headerlink" title="5.2 索引搜索"></a>5.2 索引搜索</h3><p>为了搜索展示方便，先想ES 中添加一些数据：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">PUT IP 地址<span class="token operator">:</span><span class="token number">9200</span>/myindex/_doc/<span class="token number">1</span>
<span class="token punctuation">{</span>
    <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"海贼王"</span><span class="token punctuation">,</span>
    <span class="token property">"content"</span><span class="token operator">:</span><span class="token string">"船长是路飞，副船长是索隆~"</span>
<span class="token punctuation">}</span>

PUT IP 地址<span class="token operator">:</span><span class="token number">9200</span>/myindex/_doc/<span class="token number">2</span>
<span class="token punctuation">{</span>
    <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"火影忍者"</span><span class="token punctuation">,</span>
    <span class="token property">"content"</span><span class="token operator">:</span><span class="token string">"七代火影是鸣人，八代火影是木叶丸~"</span>
<span class="token punctuation">}</span>

PUT IP 地址<span class="token operator">:</span><span class="token number">9200</span>/myindex/_doc/<span class="token number">3</span>
<span class="token punctuation">{</span>
    <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"一拳超人"</span><span class="token punctuation">,</span>
    <span class="token property">"content"</span><span class="token operator">:</span><span class="token string">"主角是埼玉，配角是杰诺斯~"</span>
<span class="token punctuation">}</span>

PUT IP 地址<span class="token operator">:</span><span class="token number">9200</span>/myindex/_doc/<span class="token number">4</span>
<span class="token punctuation">{</span>
    <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"进击的巨人"</span><span class="token punctuation">,</span>
    <span class="token property">"content"</span><span class="token operator">:</span><span class="token string">"人类最牛的是兵长，巨人最菜的是男主~"</span>
<span class="token punctuation">}</span>

PUT IP 地址<span class="token operator">:</span><span class="token number">9200</span>/myindex/_doc/<span class="token number">5</span>
<span class="token punctuation">{</span>
    <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"名侦探柯南"</span><span class="token punctuation">,</span>
    <span class="token property">"content"</span><span class="token operator">:</span><span class="token string">"男主是柯南，女主不确定~"</span>
<span class="token punctuation">}</span>

PUT IP 地址<span class="token operator">:</span><span class="token number">9200</span>/myindex/_doc/<span class="token number">6</span>
<span class="token punctuation">{</span>
    <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"海贼王路飞"</span><span class="token punctuation">,</span>
    <span class="token property">"content"</span><span class="token operator">:</span><span class="token string">"路飞是要成为海贼我的男人~"</span>
<span class="token punctuation">}</span>

PUT IP 地址<span class="token operator">:</span><span class="token number">9200</span>/myindex/_doc/<span class="token number">7</span>
<span class="token punctuation">{</span>
    <span class="token property">"title"</span><span class="token operator">:</span><span class="token string">"路飞的果实能力"</span><span class="token punctuation">,</span>
    <span class="token property">"content"</span><span class="token operator">:</span><span class="token string">"路飞是吃了橡胶果实的男人~"</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="搜索全部"><a href="#搜索全部" class="headerlink" title="搜索全部"></a>搜索全部</h4><p><code>GET IP地址:9200/myindex/_search</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130192839909.png" alt="在这里插入图片描述"></p>
<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">{</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">1033</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">7</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"5"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"名侦探柯南"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"男主是柯南，女主不确定~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"4"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"进击的巨人"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"人类最牛的是兵长，巨人最菜的是男主~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"3"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"一拳超人"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"主角是埼玉，配角是杰诺斯~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"2"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"火影忍者"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"七代火影是鸣人，八代火影是木叶丸~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"海贼王"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"船长是路飞，副船长是索隆~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"海贼王路飞"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"路飞是要成为海贼我的男人~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"7"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"路飞的果实能力"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"路飞是吃了橡胶果实的男人~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="根据单个条件搜索"><a href="#根据单个条件搜索" class="headerlink" title="根据单个条件搜索"></a>根据单个条件搜索</h4><p><code>GET IP地址:9200/myindex/_search?q=title:海贼王</code></p>
<p>或者</p>
<p><code>GET IP地址:9200/myindex/_search?q=content:路飞</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130193338883.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">{</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1.7349373</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.7349373</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"海贼王"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"船长是路飞，副船长是索隆~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.6770141</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"海贼王路飞"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"路飞是要成为海贼我的男人~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"7"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.6770141</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"路飞的果实能力"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"路飞是吃了橡胶果实的男人~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="根据多个条件搜索"><a href="#根据多个条件搜索" class="headerlink" title="根据多个条件搜索"></a>根据多个条件搜索</h4><p><code>GET IP地址:9200/myindex/_search</code></p>
<p><img src="https://img-blog.csdnimg.cn/20201130193741896.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">{</span>
    <span class="token property">"query"</span><span class="token operator">:</span><span class="token punctuation">{</span>
        <span class="token comment">// 多个匹配</span>
        <span class="token property">"multi_match"</span><span class="token operator">:</span><span class="token punctuation">{</span>
            <span class="token comment">// 查询条件为 路飞</span>
            <span class="token property">"query"</span><span class="token operator">:</span><span class="token string">"路飞"</span><span class="token punctuation">,</span>
            <span class="token comment">// 从title 和 content 字段中匹配是否存在 路飞 的数据</span>
            <span class="token property">"fields"</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token string">"title"</span><span class="token punctuation">,</span><span class="token string">"content"</span><span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>返回结果：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">{</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">32</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">2.2700202</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"6"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">2.2700202</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"海贼王路飞"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"路飞是要成为海贼我的男人~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"7"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.9412584</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"路飞的果实能力"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"路飞是吃了橡胶果实的男人~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"myindex"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"1"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.7349373</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"海贼王"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"船长是路飞，副船长是索隆~"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>如果要了解更多命令，可以参考这篇文章：<a target="_blank" rel="noopener" href="https://blog.csdn.net/vtopqx/article/details/105998990">https://blog.csdn.net/vtopqx/article/details/105998990</a></p>
<h2 id="6-SpringBoot-整合-ES"><a href="#6-SpringBoot-整合-ES" class="headerlink" title="6. SpringBoot 整合 ES"></a>6. SpringBoot 整合 ES</h2><h3 id="6-1-引入依赖"><a href="#6-1-引入依赖" class="headerlink" title="6.1 引入依赖"></a>6.1 引入依赖</h3><p><strong>pom.xml</strong> 中引入依赖</p>
<pre class="line-numbers language-xml" data-language="xml"><code class="language-xml"><span class="token comment">&lt;!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-data-elasticsearch<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.2.5.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="6-2-添加-ES-相关配置"><a href="#6-2-添加-ES-相关配置" class="headerlink" title="6.2 添加 ES 相关配置"></a>6.2 添加 ES 相关配置</h3><p><strong>application.yml</strong> 中配置</p>
<pre class="line-numbers language-yaml" data-language="yaml"><code class="language-yaml"><span class="token comment"># spring 相关配置</span>
<span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token comment"># elasticsearch 配置</span>
  <span class="token key atrule">elasticsearch</span><span class="token punctuation">:</span>
    <span class="token key atrule">rest</span><span class="token punctuation">:</span>
      <span class="token comment"># ip是服务器ip地址</span>
      <span class="token key atrule">uris</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//8.xxx.xx.45<span class="token punctuation">:</span><span class="token number">9200</span>
      <span class="token comment"># 我没有设置账号密码,所以下面可以不配置</span>
      <span class="token comment">#username:</span>
      <span class="token comment">#password:</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="6-3-相关注解介绍"><a href="#6-3-相关注解介绍" class="headerlink" title="6.3 相关注解介绍"></a>6.3 相关注解介绍</h3><blockquote>
<p>ES 几个常用注解</p>
</blockquote>
<ul>
<li><code>@Document</code>：声明索引库配置<ul>
<li><code>indexName</code>：索引库名称</li>
<li><code>type</code>：映射类型。如果未设置，则使用小写的类的简单名称。（<strong>从版本4.0开始不推荐使用</strong>）</li>
<li><code>shards</code>：分片数量，默认 <strong>5</strong></li>
<li><code>replicas</code>：副本数量，默认 <strong>1</strong></li>
</ul>
</li>
<li><code>@Id</code>：声明实体类的id</li>
<li><code>@Field</code>：声明字段属性<ul>
<li><code>type</code>：字段的数据类型</li>
<li><code>analyzer</code>：指定在存储时候使用的分词器类型</li>
<li><code>searchAnalyzer</code>：指定在搜索时候使用的分词器类型</li>
<li><code>index</code>：是否创建索引</li>
</ul>
</li>
</ul>
<h3 id="6-4-实体类"><a href="#6-4-实体类" class="headerlink" title="6.4 实体类"></a>6.4 实体类</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/**
 * @Auther: csp1999
 * @Date: 2020/11/24/8:55
 * @Description: 帖子实体类
 */</span>
<span class="token comment">// Lombok 相关注解</span>
<span class="token annotation punctuation">@Data</span>
<span class="token annotation punctuation">@AllArgsConstructor</span>
<span class="token annotation punctuation">@NoArgsConstructor</span>
<span class="token annotation punctuation">@Accessors</span><span class="token punctuation">(</span>chain <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@ToString</span>
<span class="token comment">// ES 相关注解</span>
<span class="token annotation punctuation">@Document</span><span class="token punctuation">(</span>indexName <span class="token operator">=</span> <span class="token string">"discusspost"</span><span class="token punctuation">,</span>shards <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">,</span>replicas <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token comment">// discusspost 必须全小写</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DiscussPost</span> <span class="token punctuation">{</span>

    <span class="token comment">/**
     * 主键id
     */</span>
    <span class="token annotation punctuation">@Id</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> id<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 用户主键id
     */</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Long</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> userId<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 帖子标题
     */</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Text</span><span class="token punctuation">,</span>analyzer <span class="token operator">=</span> <span class="token string">"ik_max_word"</span><span class="token punctuation">,</span>searchAnalyzer <span class="token operator">=</span> <span class="token string">"ik_smart"</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> title<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 帖子内容
     */</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Text</span><span class="token punctuation">,</span>analyzer <span class="token operator">=</span> <span class="token string">"ik_max_word"</span><span class="token punctuation">,</span>searchAnalyzer <span class="token operator">=</span> <span class="token string">"ik_smart"</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> content<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 帖子类型
     * 0-普通; 1-置顶;
     */</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Integer</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> type<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 帖子状态
     * 0-正常; 1-精华; 2-拉黑;
     */</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Integer</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> status<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 帖子创建日期
     */</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Date</span><span class="token punctuation">,</span> format <span class="token operator">=</span> <span class="token class-name">DateFormat</span><span class="token punctuation">.</span>custom<span class="token punctuation">,</span>pattern <span class="token operator">=</span> <span class="token string">"yyyy-MM-dd"</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@JsonFormat</span><span class="token punctuation">(</span>shape <span class="token operator">=</span> <span class="token class-name">JsonFormat<span class="token punctuation">.</span>Shape</span><span class="token punctuation">.</span>STRING<span class="token punctuation">,</span> pattern <span class="token operator">=</span><span class="token string">"yyyy-MM-dd"</span><span class="token punctuation">,</span>timezone<span class="token operator">=</span><span class="token string">"GMT+8"</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">Date</span> createTime<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 帖子评论数量
     */</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Integer</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> commentCount<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 帖子得分
     */</span>
    <span class="token annotation punctuation">@Field</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">FieldType<span class="token punctuation">.</span>Float</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token keyword">double</span> score<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="6-5-Repository-接口"><a href="#6-5-Repository-接口" class="headerlink" title="6.5 Repository 接口"></a>6.5 Repository 接口</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/**
 * @Auther: csp1999
 * @Date: 2020/11/30/21:01
 * @Description: 帖子相关的Repository
 */</span>
<span class="token annotation punctuation">@Repository</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">DiscussPostRepository</span> <span class="token keyword">extends</span> <span class="token class-name">ElasticsearchRepository</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">DiscussPost</span><span class="token punctuation">,</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
    
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="执行测试"><a href="#执行测试" class="headerlink" title="执行测试"></a>执行测试</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/**
 * @Auther: csp1999
 * @Date: 2020/11/30/21:04
 * @Description: ES 测试
 */</span>
<span class="token annotation punctuation">@SpringBootTest</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ElasticSearchTest</span> <span class="token punctuation">{</span>

    <span class="token comment">/**
     * 从mysql数据中获取数据
     */</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">DiscussPostMapper</span> discussPostMapper<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 注入 DiscussPostRepository
     */</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">DiscussPostRepository</span> discussPostRepository<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 有些情况 DiscussPostRepository 处理不了
     * 所以需要额外的 ElasticsearchRestTemplate
     */</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">ElasticsearchRestTemplate</span> elasticsearchRestTemplate<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testInsert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// 从数据库查询 id 为217 282 283 284 的帖子存入 ES 中</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPostById</span><span class="token punctuation">(</span><span class="token number">217</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPostById</span><span class="token punctuation">(</span><span class="token number">282</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPostById</span><span class="token punctuation">(</span><span class="token number">283</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPostById</span><span class="token punctuation">(</span><span class="token number">284</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>运行测试代码，如果执行成功后，使用Postman 测试查看 ES 中的索引库内容：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130214524121.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>可以看出，索引库已经创建好了！</p>
<p>我们来查一些数据内容：</p>
<p><img src="https://img-blog.csdnimg.cn/2020113021491421.png" alt="在这里插入图片描述"></p>
<p>结果为：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">{</span>
    <span class="token property">"took"</span><span class="token operator">:</span> <span class="token number">249</span><span class="token punctuation">,</span>
    <span class="token property">"timed_out"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token property">"_shards"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
        <span class="token property">"successful"</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
        <span class="token property">"skipped"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token property">"failed"</span><span class="token operator">:</span> <span class="token number">0</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">"total"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token property">"value"</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
            <span class="token property">"relation"</span><span class="token operator">:</span> <span class="token string">"eq"</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token property">"max_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
        <span class="token property">"hits"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"discusspost"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"282"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"_class"</span><span class="token operator">:</span> <span class="token string">"com.haust.community.pojo.DiscussPost"</span><span class="token punctuation">,</span>
                    <span class="token property">"id"</span><span class="token operator">:</span> <span class="token number">282</span><span class="token punctuation">,</span>
                    <span class="token property">"userId"</span><span class="token operator">:</span> <span class="token number">152</span><span class="token punctuation">,</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"哈哈***哈哈"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"***哈哈"</span><span class="token punctuation">,</span>
                    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                    <span class="token property">"status"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                    <span class="token property">"createTime"</span><span class="token operator">:</span> <span class="token string">"2020-11-25"</span><span class="token punctuation">,</span>
                    <span class="token property">"commentCount"</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                    <span class="token property">"score"</span><span class="token operator">:</span> <span class="token number">0.0</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"discusspost"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"284"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"_class"</span><span class="token operator">:</span> <span class="token string">"com.haust.community.pojo.DiscussPost"</span><span class="token punctuation">,</span>
                    <span class="token property">"id"</span><span class="token operator">:</span> <span class="token number">284</span><span class="token punctuation">,</span>
                    <span class="token property">"userId"</span><span class="token operator">:</span> <span class="token number">153</span><span class="token punctuation">,</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"海贼王我当定了！"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"海贼王我当定了！"</span><span class="token punctuation">,</span>
                    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                    <span class="token property">"status"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                    <span class="token property">"createTime"</span><span class="token operator">:</span> <span class="token string">"2020-11-29"</span><span class="token punctuation">,</span>
                    <span class="token property">"commentCount"</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
                    <span class="token property">"score"</span><span class="token operator">:</span> <span class="token number">0.0</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"discusspost"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"217"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"_class"</span><span class="token operator">:</span> <span class="token string">"com.haust.community.pojo.DiscussPost"</span><span class="token punctuation">,</span>
                    <span class="token property">"id"</span><span class="token operator">:</span> <span class="token number">217</span><span class="token punctuation">,</span>
                    <span class="token property">"userId"</span><span class="token operator">:</span> <span class="token number">103</span><span class="token punctuation">,</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"互联网求职暖春计划"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"今年的就业形势，确实不容乐观。过了个年，仿佛跳水一般，整个讨论区哀鸿遍野！19届真的没人要了吗？！18届被优化真的没有出路了吗？！大家的“哀嚎”与“悲惨遭遇”牵动了每日潜伏于讨论区的牛客小哥哥小姐姐们的心，于是牛客决定：是时候为大家做点什么了！为了帮助大家度过“艰难”，牛客网特别联合60+家企业，开启互联网求职暖春计划，面向18届&amp;19届，拯救0 offer！"</span><span class="token punctuation">,</span>
                    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                    <span class="token property">"status"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                    <span class="token property">"createTime"</span><span class="token operator">:</span> <span class="token string">"2019-04-04"</span><span class="token punctuation">,</span>
                    <span class="token property">"commentCount"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                    <span class="token property">"score"</span><span class="token operator">:</span> <span class="token number">0.0</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">"_index"</span><span class="token operator">:</span> <span class="token string">"discusspost"</span><span class="token punctuation">,</span>
                <span class="token property">"_type"</span><span class="token operator">:</span> <span class="token string">"_doc"</span><span class="token punctuation">,</span>
                <span class="token property">"_id"</span><span class="token operator">:</span> <span class="token string">"283"</span><span class="token punctuation">,</span>
                <span class="token property">"_score"</span><span class="token operator">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span>
                <span class="token property">"_source"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"_class"</span><span class="token operator">:</span> <span class="token string">"com.haust.community.pojo.DiscussPost"</span><span class="token punctuation">,</span>
                    <span class="token property">"id"</span><span class="token operator">:</span> <span class="token number">283</span><span class="token punctuation">,</span>
                    <span class="token property">"userId"</span><span class="token operator">:</span> <span class="token number">152</span><span class="token punctuation">,</span>
                    <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"海贼王我当定了！"</span><span class="token punctuation">,</span>
                    <span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"Hello World!"</span><span class="token punctuation">,</span>
                    <span class="token property">"type"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                    <span class="token property">"status"</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                    <span class="token property">"createTime"</span><span class="token operator">:</span> <span class="token string">"2020-11-25"</span><span class="token punctuation">,</span>
                    <span class="token property">"commentCount"</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
                    <span class="token property">"score"</span><span class="token operator">:</span> <span class="token number">0.0</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="6-6-SpringBoot-操作ES增删改"><a href="#6-6-SpringBoot-操作ES增删改" class="headerlink" title="6.6 SpringBoot 操作ES增删改"></a>6.6 SpringBoot 操作ES增删改</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">/**
 * @Auther: csp1999
 * @Date: 2020/11/30/21:04
 * @Description: ES 测试
 */</span>
<span class="token annotation punctuation">@SpringBootTest</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ElasticSearchTest</span> <span class="token punctuation">{</span>

    <span class="token comment">/**
     * 从mysql数据中获取数据
     */</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">DiscussPostMapper</span> discussPostMapper<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 注入 DiscussPostRepository
     */</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">DiscussPostRepository</span> discussPostRepository<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 有些情况 DiscussPostRepository 处理不了
     * 所以需要额外的 ElasticsearchRestTemplate
     */</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">ElasticsearchRestTemplate</span> elasticsearchRestTemplate<span class="token punctuation">;</span>

    <span class="token comment">/**
     * 创建/更新索引
     */</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testCreateIndex</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token class-name">IndexOperations</span> indexOperations <span class="token operator">=</span> elasticsearchRestTemplate<span class="token punctuation">.</span><span class="token function">indexOps</span><span class="token punctuation">(</span><span class="token class-name">DiscussPost</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Document</span> document <span class="token operator">=</span> indexOperations<span class="token punctuation">.</span><span class="token function">createMapping</span><span class="token punctuation">(</span><span class="token class-name">DiscussPost</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//boolean bool = indexOperations.create(document);</span>
        <span class="token keyword">boolean</span> bool <span class="token operator">=</span> indexOperations<span class="token punctuation">.</span><span class="token function">putMapping</span><span class="token punctuation">(</span>document<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bool<span class="token operator">?</span><span class="token string">"成功"</span><span class="token operator">:</span><span class="token string">"失败"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * 每次只插入一条
     */</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testInsert</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 从数据库查询 id 为217 282 283 284 的帖子存入 ES 中</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPostById</span><span class="token punctuation">(</span><span class="token number">217</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPostById</span><span class="token punctuation">(</span><span class="token number">282</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPostById</span><span class="token punctuation">(</span><span class="token number">283</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPostById</span><span class="token punctuation">(</span><span class="token number">284</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * 批量插入
     */</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testInsertAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">saveAll</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPosts</span><span class="token punctuation">(</span><span class="token number">101</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">saveAll</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPosts</span><span class="token punctuation">(</span><span class="token number">102</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">saveAll</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPosts</span><span class="token punctuation">(</span><span class="token number">103</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">saveAll</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPosts</span><span class="token punctuation">(</span><span class="token number">152</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">saveAll</span><span class="token punctuation">(</span>discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPosts</span><span class="token punctuation">(</span><span class="token number">153</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * 测试修改
     */</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">DiscussPost</span> discussPost <span class="token operator">=</span> discussPostMapper<span class="token punctuation">.</span><span class="token function">selectDiscussPostById</span><span class="token punctuation">(</span><span class="token number">203</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPost<span class="token punctuation">.</span><span class="token function">setContent</span><span class="token punctuation">(</span><span class="token string">"海贼王，路飞当定了~"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>discussPost<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * 测试删除
     */</span>
    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testDelete</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// discussRepository.deleteById(203);</span>
        discussPostRepository<span class="token punctuation">.</span><span class="token function">deleteAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//全部删除</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="6-7-SpringBoot-操作ES-高亮搜索-核心"><a href="#6-7-SpringBoot-操作ES-高亮搜索-核心" class="headerlink" title="6.7 SpringBoot 操作ES 高亮搜索(核心)"></a>6.7 SpringBoot 操作ES 高亮搜索(核心)</h3><p>效果如图：</p>
<p><img src="https://img-blog.csdnimg.cn/20201130223547480.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzU5MTk4MA==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>将从ES 中查询的内容，把搜索的关键字高亮显示借助<em> 标签：</em></p><em>
<p>参考代码：</p>
<pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testSearchByTemplate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 构建查询条件</span>
    <span class="token class-name">NativeSearchQuery</span> searchQuery <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NativeSearchQueryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">withQuery</span><span class="token punctuation">(</span><span class="token class-name">QueryBuilders</span><span class="token punctuation">.</span><span class="token function">multiMatchQuery</span><span class="token punctuation">(</span><span class="token string">"互联网寒冬"</span><span class="token punctuation">,</span> <span class="token string">"title"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">withSort</span><span class="token punctuation">(</span><span class="token class-name">SortBuilders</span><span class="token punctuation">.</span><span class="token function">fieldSort</span><span class="token punctuation">(</span><span class="token string">"type"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">order</span><span class="token punctuation">(</span><span class="token class-name">SortOrder</span><span class="token punctuation">.</span>DESC<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">// 按照帖子分类排序</span>
            <span class="token punctuation">.</span><span class="token function">withSort</span><span class="token punctuation">(</span><span class="token class-name">SortBuilders</span><span class="token punctuation">.</span><span class="token function">fieldSort</span><span class="token punctuation">(</span><span class="token string">"score"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">order</span><span class="token punctuation">(</span><span class="token class-name">SortOrder</span><span class="token punctuation">.</span>DESC<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">// 按照帖子分数排序</span>
            <span class="token punctuation">.</span><span class="token function">withSort</span><span class="token punctuation">(</span><span class="token class-name">SortBuilders</span><span class="token punctuation">.</span><span class="token function">fieldSort</span><span class="token punctuation">(</span><span class="token string">"createTime"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">order</span><span class="token punctuation">(</span><span class="token class-name">SortOrder</span><span class="token punctuation">.</span>DESC<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">// 按照帖子发布日期排序</span>
            <span class="token punctuation">.</span><span class="token function">withPageable</span><span class="token punctuation">(</span><span class="token class-name">PageRequest</span><span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">// 每页十条数据</span>
            <span class="token punctuation">.</span><span class="token function">withHighlightFields</span><span class="token punctuation">(</span>
                    <span class="token comment">// 标题和内容中的匹配字段高亮展示</span>
                    <span class="token keyword">new</span> <span class="token class-name">HighlightBuilder<span class="token punctuation">.</span>Field</span><span class="token punctuation">(</span><span class="token string">"title"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">preTags</span><span class="token punctuation">(</span><span class="token string">"&lt;em&gt;"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">postTags</span><span class="token punctuation">(</span><span class="token string">"&lt;/em&gt;"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                    <span class="token keyword">new</span> <span class="token class-name">HighlightBuilder<span class="token punctuation">.</span>Field</span><span class="token punctuation">(</span><span class="token string">"content"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">preTags</span><span class="token punctuation">(</span><span class="token string">"&lt;em&gt;"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">postTags</span><span class="token punctuation">(</span><span class="token string">"&lt;/em&gt;"</span><span class="token punctuation">)</span>
            <span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 得到查询结果返回容纳指定内容对象的集合SearchHits</span>
    <span class="token class-name">SearchHits</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">DiscussPost</span><span class="token punctuation">&gt;</span></span> searchHits <span class="token operator">=</span> elasticsearchRestTemplate<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span>searchQuery<span class="token punctuation">,</span> <span class="token class-name">DiscussPost</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 设置一个需要返回的实体类集合</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">DiscussPost</span><span class="token punctuation">&gt;</span></span> discussPosts <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 遍历返回的内容进行处理</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">SearchHit</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">DiscussPost</span><span class="token punctuation">&gt;</span></span> searchHit <span class="token operator">:</span> searchHits<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 高亮的内容</span>
        <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> highlightFields <span class="token operator">=</span> searchHit<span class="token punctuation">.</span><span class="token function">getHighlightFields</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 将高亮的内容填充到content中</span>
        searchHit<span class="token punctuation">.</span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setTitle</span><span class="token punctuation">(</span>highlightFields<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"title"</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">?</span> 
                searchHit<span class="token punctuation">.</span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTitle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> highlightFields<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"title"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        searchHit<span class="token punctuation">.</span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setTitle</span><span class="token punctuation">(</span>highlightFields<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"content"</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">?</span> 
                searchHit<span class="token punctuation">.</span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> highlightFields<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"content"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 放到实体类中</span>
        discussPosts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>searchHit<span class="token punctuation">.</span><span class="token function">getContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 输出结果</span>
    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>discussPosts<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">DiscussPost</span> discussPost <span class="token operator">:</span> discussPosts<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>discussPost<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>参考相关文章：</p>
<ul>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_40885085/article/details/105024625">https://blog.csdn.net/qq_40885085/article/details/105024625</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_42385395/article/details/109315087">https://blog.csdn.net/weixin_42385395/article/details/109315087</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/yj1499945/article/details/107726223">https://blog.csdn.net/yj1499945/article/details/107726223</a></p>
</li>
<li><p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/hi3254014978/archive/2004/01/13/14055771.html">https://www.cnblogs.com/hi3254014978/archive/2004/01/13/14055771.html</a></p>
</li>
</ul>
</em>
                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/hexo-blog/about" rel="external nofollow noreferrer">兴趣使然の草帽路飞</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://caoshipeng.gitee.io/hexo-blog/hexo-blog/2021/02/05/linux-huan-jing-xia-elasticsearch-de-an-zhuang-yu-shi-yong-springboot-zheng-he-yun-fu-wu-qi-shang-de-elasticsearch/">https://caoshipeng.gitee.io/hexo-blog/hexo-blog/2021/02/05/linux-huan-jing-xia-elasticsearch-de-an-zhuang-yu-shi-yong-springboot-zheng-he-yun-fu-wu-qi-shang-de-elasticsearch/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/hexo-blog/about" target="_blank">兴趣使然の草帽路飞</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/hexo-blog/tags/ElasticSearch/">
                                    <span class="chip bg-color">ElasticSearch</span>
                                </a>
                            
                                <a href="/hexo-blog/tags/Linux/">
                                    <span class="chip bg-color">Linux</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/hexo-blog/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="qq,qzone,wechat,weibo" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/hexo-blog/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/hexo-blog/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/hexo-blog/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }

    #vcomments textarea {
        box-sizing: border-box;
        background: url("/hexo-blog/medias/comment_bg.png") 100% 100% no-repeat;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #4cbf30;
        font-weight: 500;
        text-decoration: none;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
        display: list-item;
        line-height: 1.8rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }
</style>

<div class="card valine-card" data-aos="fade-up">
    <div class="comment_headling" style="font-size: 20px; font-weight: 700; position: relative; padding-left: 20px; top: 15px; padding-bottom: 5px;">
        <i class="fas fa-comments fa-fw" aria-hidden="true"></i>
        <span>评论</span>
    </div>
    <div id="vcomments" class="card-content" style="display: grid">
    </div>
</div>

<script src="/hexo-blog/libs/valine/av-min.js"></script>
<script src="/hexo-blog/libs/valine/Valine.min.js"></script>
<script>
    new Valine({
        el: '#vcomments',
        appId: 'nqtLczfzJWfstaxJjohOkRxK-gzGzoHsz',
        appKey: 'yNBTs2KvEWvNvV5WoiTARROX',
        notify: 'false' === 'true',
        verify: 'false' === 'true',
        visitor: 'true' === 'true',
        avatar: 'mm',
        pageSize: '10',
        lang: 'zh-cn',
        placeholder: 'just go go'
    });
</script>

<!--酷Q推送-->


    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/hexo-blog/2021/02/05/zi-ding-yi-fen-ye-gong-ju-lei-fang-bai-du-sou-suo-fen-ye-fang-shi/">
                    <div class="card-image">
                        
                        
                        <img src="/hexo-blog/medias/featureimages/9.jpg" class="responsive-img" alt="自定义全局Page工具类">
                        
                        <span class="card-title">自定义全局Page工具类</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            SpringBoot结合Thymleaf自定义全局Page工具类
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-02-05
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/hexo-blog/categories/%E5%90%8E%E7%AB%AF/" class="post-category">
                                    后端
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/hexo-blog/tags/SpringBoot/">
                        <span class="chip bg-color">SpringBoot</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/hexo-blog/2021/02/05/springboot-fang-niu-ke-lun-tan-xiang-mu-shi-zhan/">
                    <div class="card-image">
                        
                        
                        <img src="/hexo-blog/medias/featureimages/23.jpg" class="responsive-img" alt="牛客论坛项目实战">
                        
                        <span class="card-title">牛客论坛项目实战</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            SpringBoot仿牛客论坛项目实战
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-02-05
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/hexo-blog/categories/%E5%90%8E%E7%AB%AF/" class="post-category">
                                    后端
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/hexo-blog/tags/SpringBoot/">
                        <span class="chip bg-color">SpringBoot</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/hexo-blog/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/hexo-blog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/hexo-blog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/hexo-blog/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/hexo-blog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/hexo-blog/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        font-size: 15px;
        color: #42b983;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="3022496969"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/hexo-blog/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021</span>
            
            <span id="year">2021</span>
            <a href="/hexo-blog/about" target="_blank">兴趣使然の草帽路飞</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">42.7k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2021";
                    var startMonth = "2";
                    var startDate = "5";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/CodeFarmer1999" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:1165680007@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1165680007" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1165680007" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/hexo-blog/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/hexo-blog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/hexo-blog/libs/materialize/materialize.min.js"></script>
    <script src="/hexo-blog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/hexo-blog/libs/aos/aos.js"></script>
    <script src="/hexo-blog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/hexo-blog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/hexo-blog/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/hexo-blog/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/hexo-blog/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    
    <script type="text/javascript" color="0,0,255"
        pointColor="0,0,255" opacity='0.7'
        zIndex="-1" count="99"
        src="/hexo-blog/libs/background/canvas-nest.js"></script>
    

    
    
    <script type="text/javascript" size="150" alpha='0.6'
        zIndex="-1" src="/hexo-blog/libs/background/ribbon.min.js" async="async"></script>
    

    
    <script type="text/javascript" src="/hexo-blog/libs/background/ribbon-dynamic.js" async="async"></script>
    

    
    <script src="/hexo-blog/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
